import QtQuick
import HBaiduPan
import HLocalDir
import QtQuick.Controls

Rectangle {
    id: window
    width: 1024
    height: 768
    visible: true
    //本地目录当前目录
    property string lpath: ""
    //拖动上传目录
    property string dragUploadPath: ""
    //盘目录当前目录
    property string spath: "/"
    //盘容量信息组件
    property QtObject quota: null
    //复制的盘路径
    property string panPath: ""
    //复制的盘文件名
    property string panFileName: ""
    //复制的是否移动
    property bool panCopy: false

    BaiduPan { //c++对象
        id:cpp_pan
    }
    LocalDir { //c++对象
        id:cpp_local
    }

    TransferUI { //上传下载界面
        id: transfer
        visible: false
    }

    CmpUI { //比较文件界面
        id: cmpui
        visible: false
    }

    ToolTip { //提示文字
        id: htooltip
        timeout: 3000
    }

    FontLoader { //加载字体图标
        id: fontello
        source: "resource/fontello.ttf"
    }

    Button { //打开上传下载界面
        anchors.left: parent.left;anchors.leftMargin: 10;font.family: "fontello";text: "\uE807";leftPadding: 6;rightPadding: 6
        onClicked: {
            transfer.visible = true;
        }
    }

    Menu { //盘右键操作菜单
        id: rightmenu
        MenuItem {
            text: "删除"
            onClicked: {
                let ret = cpp_pan.remove(spath + "/" + pandir.model[pandir.currentIndex].server_filename);
                ret = JSON.parse(ret);
                if(ret.errno === 0){
                    panreload.clicked();
                }
            }
        }
        MenuItem {
            text: "复制"
            onClicked: {
                panCopy = true;
                panPath = spath;
                panFileName = pandir.model[pandir.currentIndex].server_filename;
            }
        }
        MenuItem {
            text: "剪切"
            onClicked: {
                panCopy = false;
                panPath = spath;
                panFileName = pandir.model[pandir.currentIndex].server_filename;
            }
        }
        MenuItem {
            text: "重命名"
            onClicked: {

            }
        }
    }
    Item { //用户信息
        width: window.width - 100;height: 20;anchors.right: window.right;anchors.rightMargin: 10
        Image {
            id: avatar_url
            width: 20;height: 20;source: "";anchors.right: parent.right;anchors.rightMargin: baiduname.width + 10
        }
        Text {
            id: baiduname
            text: qsTr("testname")
            anchors.right: parent.right
        }
        MouseArea { //显示网盘容量信息
            anchors.fill: parent
            onClicked: function(e){
                //console.log(e.x, e.y);
                if(!quota){
                    let comp = Qt.createComponent("QuotaUI.qml");
                    quota = comp.createObject(window);
                    quota.x = e.x - 50 + 100;
                    quota.y = e.y + 10;
                    //console.log(comp, comp.text);
                    let val = JSON.parse(cpp_pan.quota());
                    val.total = val.total / 1024 / 1024 / 1024;
                    val.used = val.used / 1024 / 1024;
                    if(val.used > 1024){
                        val.used = (val.used / 1024|0) + 'G';
                    }else{
                        val.used = (val.used|0) + 'M';
                    }
                    if(val.total > 1024){
                        val.total = (val.total / 1024|0) + 'T';
                    }else{
                        val.total = (val.total|0) + 'G';
                    }
                    quota.text = "容量:" + val.used + "/" + val.total;
                    console.log(quota, quota.text);
                    //console.log(val);
                }else{
                    quota.destroy();
                    quota = null;
                }
            }
        }
    }

    Rectangle { //本地文件
        width: window.width / 2 - 30; height: window.height - 30; anchors.top: window.top; anchors.topMargin: 30; anchors.left: window.left; anchors.leftMargin: 5; color: "#eee"
        //border.width: 1

        Row { //操作工具栏
            anchors.top: parent.top; anchors.topMargin: 5; anchors.left: parent.left; anchors.leftMargin: 10; width: parent.width - 10;
            Button { //刷新按钮
                id: localreload
                font.family: "fontello"; text: "\uE800"; leftPadding: 6; rightPadding: 6;
                onClicked: {
                    let list = cpp_local.list(lpath);
                    localdir.model = JSON.parse(list);
                    localdir.currentIndex = -1;
                }
            }
            Button { //上层目录
                font.family: "fontello"; text: "\uE801"; leftPadding: 6; rightPadding: 6;
                onClicked: {
                    let pd = lpath.split("/");
                    if(pd.length > 1){
                        pd.length = pd.length - 1;
                        localpath.model = pd.slice(0).reverse();
                        lpath = pd.join("/");
                        //显示目录下内容
                        //let list = cpp_local.list(lpath);
                        //localdir.model = JSON.parse(list);
                        localreload.clicked();
                    }else{
                        //测试时候window下访问盘时我项目放E盘的一个目录中就会导致我访问E盘变成访问调试目录
                        localpath.model = [];
                        lpath = "";
                        //显示目录下内容
                        let list = cpp_local.drives();
                        localdir.model = JSON.parse(list);
                        localdir.currentIndex = -1;
                    }
                }
            }
            Row { //目录
                width: parent.width - 44;height:parent.height;clip:true;
                layoutDirection: Qt.RightToLeft
                Repeater {
                    id: localpath
                    model: ["D:","works"]
                    Text {
                        anchors.top: parent.top;
                        anchors.topMargin: 5
                        text: modelData + "/"
                    }
                }
            }
        }
        ListView {
            id: localdir
            height: parent.height - 30;
            width: parent.width - 5;
            anchors.top: parent.top;
            anchors.topMargin: 30;
            anchors.left: parent.left;
            anchors.leftMargin: 5;
            focus: true;
            //interactive: false; //添加这行代码后 不能滚动了 忘了当时为什么加了
            currentIndex: -1
            highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
            delegate: Item {
                height: 20
                width: parent.width;
                //color: "#f00"
                Item { //不套这层放鼠标不恢复原位
                    id: litem
                    height: parent.height
                    width: parent.width
                    anchors.fill: parent.fill
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.horizontalCenter: parent.horizontalCenter

                    Drag.keys: ["draglocal"]
                    Drag.active: dragLocal.drag.active;
                    Drag.hotSpot.x: 10
                    Drag.hotSpot.y: 10
                    states: State {
                        when: dragLocal.drag.active
                        ParentChange { target: litem; parent: window; } //设置此代码后可以在最上层不过释放鼠标不能恢复显示了
                        AnchorChanges { target: litem; anchors.verticalCenter: undefined; anchors.horizontalCenter: undefined }
                        PropertyChanges { target: litem; z: 100 }
                    }
                    Drag.onDragStarted: {
                        console.log("onDragStarted");
                    }
                    Drag.onDragFinished: {
                        console.log("onDragFinished");
                    }

                    Image {
                        width: 20; height: 20;
                        source: "resource/" + (modelData.isDir?"box.png":"file.png")
                    }
                    Text {
                        anchors.left: parent.left; anchors.leftMargin: 25;
                        text: modelData.fileName
                    }
                    /*MouseArea {
                        id: dragLocal;
                        drag.target: litem;

                        anchors.fill: parent
                        onClicked: {
                            console.log(index);
                            if(localdir.currentIndex === index){
                                localdir.currentIndex = -1;
                            }else{
                                localdir.currentIndex = index
                            }
                        }
                        onDoubleClicked: function(e){
                            if(localdir.model[index].isDir){
                                lpath += (lpath?"/":"") + localdir.model[index].fileName;
                                localpath.model = lpath.split("/").reverse();
                                //显示目录下内容
                                //let list = cpp_local.list(lpath);
                                //localdir.model = JSON.parse(list);
                                localreload.clicked();
                            }
                        }
                    }*/
                }
            }
            MouseArea {
                id: dragLocal;
                anchors.fill: parent;
                property int dragIndex: -1
                onPressed: function(mouse){
                    console.log("onpressed", dragLocal.drag.target);
                    //if(dragItem){ console.log("onPressed", dragItem, dragItem.parent);console.log(dragItem.anchors.verticalCenter, dragItem.x, dragItem.y)}
                    let lt= localdir.itemAt(mouse.x, mouse.y);
                    if(lt !== null){
                        dragIndex = localdir.indexAt(mouse.x, mouse.y);
                        dragLocal.drag.target = lt.children[0];
                        //console.log("onPressed begin", dragItem, dragItem.parent, dragItem.anchors.verticalCenter, dragItem.x, dragItem.y);
                    }else{
                        dragLocal.drag.target = null;
                    }
                }
                onClicked: function(mouse){
                    console.log("onClicked", localdir.count);
                    //console.log("clicked", mouse.x, mouse.y, localdir.indexAt(mouse.x, mouse.y));
                    let index = localdir.indexAt(mouse.x, mouse.y);
                    if(localdir.currentIndex === index){
                        localdir.currentIndex = -1;
                    }else{
                        localdir.currentIndex = index;
                    }
                }
                onReleased: {
                    console.log("onReleased", dragLocal.drag.active, dragLocal.drag.target)
                    if(dragLocal.drag.target?.Drag?.target === panDragTarget){ //本地拖到网盘
                        console.log("localtopan", dragIndex);
                        localdir.currentIndex = dragIndex;
                        //uploadBt.clicked();
                        dragLocal.drag.target.Drag.drop(); //触发dropped事件
                    }else if(dragLocal.drag.active){ //没拖到指定位置也要强制刷新不然乱
                        let md = localdir.model;
                        localdir.model = [];
                        localdir.model = md;
                        localdir.currentIndex = -1;
                    }
                    dragLocal.drag.target = null;
                    console.log("onReleased end");
                    //强制刷新了一下数据才恢复显示 暂时这样以后再研究 这代码会导致只能拖一次
                    /*let md = localdir.model;
                    localdir.model = [];
                    localdir.model = md;
                    localdir.currentIndex = -1;*/
                }
                onDoubleClicked: function(mouse){
                    console.log("onDoubleClicked");
                    dragLocal.drag.target = null; //没这代码会导致onreleased访问已经释放的对象
                    let index = localdir.indexAt(mouse.x, mouse.y);
                    if(index === -1) return;
                    if(localdir.model[index].isDir){
                        lpath += (lpath?"/":"") + localdir.model[index].fileName;
                        localpath.model = lpath.split("/").reverse();
                        //显示目录下内容
                        //let list = cpp_local.list(lpath);
                        //localdir.model = JSON.parse(list);
                        localreload.clicked();
                    }
                }
            }
            DropArea {
                id: localDragTarget
                anchors.fill: parent
                width: parent.width
                height: parent.height
                keys: ["dragpan"] //接收盘拖过来的文件
                Rectangle {
                    id: localDragRectangle
                    anchors.fill: parent
                    opacity: 0.3
                    visible: false
                    states: [
                        State {
                            when: localDragTarget.containsDrag
                            PropertyChanges {
                                target: localDragRectangle
                                color: "grey"
                                visible: true
                            }
                        },
                        State {
                            when: panDragTarget.containsDrag == false
                            PropertyChanges {
                                target: localDragRectangle
                                color: "white"
                                visible: false
                            }
                        }
                    ]
                }
                onDropped: function(drag){ //这个只有文件浏览器拖过来的才能触发次事件
                    console.log("onDropped", drag);
                }
                onEntered: function(drag){
                    console.log("onEntered", drag.urls.length);
                }
                onExited: function(){
                    console.log("local onExited")
                    //拖动结束了才能刷新 放在这强制刷新listview就不会乱了
                    if(dragPan.drag.target == null){
                        let md = pandir.model;
                        pandir.model = [];
                        pandir.model = md;
                        pandir.currentIndex = -1;
                    }
                }
                onPositionChanged: function(mouse){
                    //console.log("onPositionChanged", mouse.x, mouse.y);
                    localdir.currentIndex = localdir.indexAt(mouse.x, mouse.y);
                }
            }
        }
    }

    Button { //对比按钮
        anchors.top: window.top; anchors.topMargin: 160; anchors.left: window.left; anchors.leftMargin: window.width / 2 - 25;
        text: ">对比<"
        onClicked: {
            if(pandir.currentIndex === -1){
                htooltip.show("必须选择对比盘目录");
                return;
            }
            if(localdir.currentIndex === -1){
                htooltip.show("必须选择对比本地目录");
                return;
            }
            let pf = pandir.model[pandir.currentIndex];
            let lf = localdir.model[localdir.currentIndex];
            if((!pf.isdir) || (!lf.isDir)){
                htooltip.show("对比的必须是目录");
                return;
            }
            let cmpl = cpp_pan.cmpPanToLocal(lpath + "/" + lf.fileName, (spath=="/"?"":spath) + "/" + pf.server_filename);
            cmpui.updateFile(JSON.parse(cmpl).list);
            let cmpp = cpp_pan.cmpLocalToPan(lpath + "/" + lf.fileName, (spath=="/"?"":spath) + "/" + pf.server_filename);
            cmpui.updateFile(JSON.parse(cmpp).list);
            cmpui.visible = true;
        }
    }

    Button { //下载按钮
        id: downBt
        anchors.top: window.top
        anchors.topMargin: 200
        anchors.left: window.left
        anchors.leftMargin: window.width / 2 - 25
        text: "下载<<"
        onClicked: {
            if(pandir.currentIndex === -1){
                htooltip.show("必须选择下载的文件");
                return;
            }
            let df = pandir.model[pandir.currentIndex];
            let lf = localdir.model[localdir.currentIndex];
            if(df.isdir){ //下载目录
                if(localdir.currentIndex !== -1 && lf.isDir){ //对比目录然后下载
                    let cmpl = cpp_pan.cmpPanToLocal(lpath + "/" + lf.fileName, (spath=="/"?"":spath) + "/" + df.server_filename);
                    //console.log(cmpl)
                    let cmprt = JSON.parse(cmpl).list;
                    if(cmprt.length > 0){
                        cmpui.updateFile(cmprt);
                        cmpui.visible = true;
                        //transfer.pushfile(JSON.parse(cmpl).list);
                    }else{
                        htooltip.show("此目录跟本地一样的不需要下载");
                    }
                }else{ //下载目录放到当前目录下
                    //判断是否存在相同目录
                    for(let lff of localdir.model){
                        if(lff.fileName === df.server_filename){
                            if(lff.isDir){
                                let cmpl = cpp_pan.cmpPanToLocal(lpath + "/" + lff.fileName, (spath=="/"?"":spath) + "/" + df.server_filename);
                                //console.log("cmp", cmpl);
                                let cmpret = JSON.parse(cmpl).list;
                                if(cmpret.length > 0){
                                    cmpui.updateFile(cmpret);
                                    cmpui.visible = true;
                                    //transfer.pushfile(JSON.parse(cmpl).list);
                                }else{
                                    htooltip.show("此目录跟本地一样的不需要下载");
                                }
                            }else{
                                htooltip.show("存在跟下载目录同名文件无法下载");
                            }
                            return;
                        }
                    }
                    //下载到当前目录下
                    let ldf = [];
                    let dfl = cpp_pan.listAll(encodeURI(spath + df.server_filename));
                    //console.log(dfl);
                    dfl = JSON.parse(dfl);
                    for(let cf of dfl.list){
                        cf.savepath = lpath + "/" + cf.path.replace(spath, "").replace("/" + cf.server_filename, "");
                        cf.sname = cf.server_filename;
                        cf.csize = 0;
                        ldf.push(cf);
                    }
                    //console.log(JSON.stringify(ldf));
                    transfer.pushfiles(ldf);
                }
            }else{ //下载文件
                if(localdir.currentIndex !== -1 && lf.isDir){
                    df.savepath = lpath + "/" + lf.fileName;
                }else{
                    df.savepath = lpath;
                }
                df.sname = df.server_filename;
                transfer.pushfile(df);
            }
        }
    }

    Button { //上传按钮
        id: uploadBt
        anchors.top: window.top
        anchors.topMargin: 240
        anchors.left: window.left
        anchors.leftMargin: window.width / 2 - 25
        text: "上传>>"
        onClicked: {
            let lf = null;
            if(dragUploadPath){
                let finfostr = cpp_local.fileInfo(dragUploadPath); //console.log(finfostr);
                lf = JSON.parse(finfostr);
            }else if(localdir.currentIndex === -1){
                htooltip.show("必须选择上传的文件");
                return;
            }else{
                lf = localdir.model[localdir.currentIndex];
            }
            if(spath.indexOf("apps/" + cpp_pan.pan_name) == -1){
                htooltip.show("上传目录只能是 " + "/apps/" + cpp_pan.pan_name);
                return;
            }
            let pf = pandir.model[pandir.currentIndex];
            if(lf.isDir){ //上传目录
                if(pandir.currentIndex !== -1 && pf.isdir){ //对比目录然后上传
                    let cmpl = cpp_pan.cmpLocalToPan(lpath + "/" + lf.fileName, (spath=="/"?"":spath) + "/" + pf.server_filename);
                    //console.log("cmp", cmpl);
                    let cmprt = JSON.parse(cmpl).list;
                    if(cmprt.length > 0){
                        cmpui.updateFile(cmprt);
                        cmpui.visible = true;
                        console.log(JSON.stringify(cmprt));
                        //transfer.pushfile(JSON.parse(cmpl).list);
                    }else{
                        htooltip.show("此目录跟本地一样的不需要上传");
                    }
                }else{ //上传目录放到当前盘目录下
                    //判断是否存在相同目录
                    for(let pff of pandir.model){
                        if(pff.server_filename === lf.fileName){
                            if(pff.isdir){
                                let cmpl = cpp_pan.cmpLocalToPan(lpath + "/" + lf.fileName, (spath=="/"?"":spath) + "/" + pff.server_filename);
                                //console.log("cmp", cmpl);
                                let cmpret = JSON.parse(cmpl).list;
                                if(cmpret.length > 0){
                                    cmpui.updateFile(cmpret);
                                    cmpui.visible = true;
                                    //transfer.pushfile(JSON.parse(cmpl).list);
                                }else{
                                    htooltip.show("此目录跟本地一样的不需要上传");
                                }
                            }else{
                                htooltip.show("存在跟上传目录同名文件无法上传");
                            }
                            return;
                        }
                    }
                    //上传到当前目录下
                    let luf = [];
                    let ufl = cpp_pan.localAll(lpath + "/" + lf.fileName);
                    //console.log(dfl);
                    ufl = JSON.parse(ufl);
                    for(let cf of ufl.list){
                        cf.savepath = spath + cf.lpath.replace(lpath, "") + "/" + cf.fileName;
                        cf.sname = cf.fileName;
                        cf.csize = 0;
                        luf.push(cf);
                    }
                    //console.log("luf", JSON.stringify(luf));
                    transfer.pushfiles(luf);
                }
            }else{ //上传文件
                if(pandir.currentIndex !== -1 && pf.isdir){ //上传到指定目录
                    lf.savepath = pf.path + "/" + lf.fileName;
                }else{
                    lf.savepath = spath + "/" + lf.fileName;
                }
                lf.csize = 0;
                lf.sname = lf.fileName;
                lf.lpath = lf.localPath;
                transfer.pushfile(lf);
            }
        }
    }

    Rectangle { //盘目录
        width: window.width / 2 - 30
        height: window.height - 30
        anchors.top: window.top
        anchors.topMargin: 30
        anchors.left: window.left
        anchors.leftMargin: window.width / 2 + 25
        //border.width: 1
        color: "#eee"

        Row {
            anchors.top: parent.top
            anchors.topMargin: 5
            anchors.left: parent.left
            anchors.leftMargin: 10
            width: parent.width; clip:true;
            Button { //刷新按钮
                id: panreload
                font.family: "fontello"
                text: "\uE800"
                leftPadding: 6
                rightPadding: 6
                onClicked: {
                    let list = cpp_pan.list(spath);
                    pandir.model = JSON.parse(list).list;
                    pandir.currentIndex = -1;
                }
            }
            Button { //上层目录
                font.family: "fontello"
                text: "\uE801"
                leftPadding: 6
                rightPadding: 6
                onClicked: {
                    let pd = spath.split("/");
                    //console.log(spath);
                    if(pd.length > 1){
                        pd.length = pd.length - 1;
                        panpath.model = pd;
                        spath = pd.join("/");
                        if(!spath) spath = "/";
                        //显示目录下内容
                        //let list = cpp_pan.list(spath);
                        //console.log(list);
                        //pandir.model = JSON.parse(list).list;
                        panreload.clicked();
                    }
                }
            }
            Button { //粘贴按钮
                enabled: (panPath?true:false)
                text: "粘贴"
                onClicked: {
                    let ret = "";
                    if(panCopy){
                        ret = cpp_pan.copy(panPath + "/" + panFileName, spath, panFileName);
                    }else{
                        ret = cpp_pan.move(panPath + "/" + panFileName, spath, panFileName);
                    }
                    ret = JSON.parse(ret);
                    if(ret.errno === 0){
                        panreload.clicked();
                        panPath = "";
                        panFileName = "";
                    }
                }
            }
            Repeater { //目录
                id: panpath
                model: [""]
                Text {
                    anchors.top: parent.top
                    anchors.topMargin: 5
                    text: modelData + "/"
                }
            }

        }

        ListView {
            id: pandir
            height: parent.height - 30;
            width: parent.width - 5;
            anchors.top: parent.top;
            anchors.topMargin: 30;
            anchors.left: parent.left;
            anchors.leftMargin: 5;
            currentIndex: -1;
            highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
            focus: true
            delegate: Item {
                height: 20
                width: parent.width
                //color: "#f00"
                Item { //不套这层放鼠标不恢复原位
                    id: pitem
                    height: parent.height
                    width: parent.width
                    anchors.fill: parent.fill
                    anchors.verticalCenter: parent.verticalCenter
                    anchors.horizontalCenter: parent.horizontalCenter

                    Drag.keys: ["dragpan"]
                    Drag.active: dragPan.drag.active;
                    Drag.hotSpot.x: 10
                    Drag.hotSpot.y: 10
                    states: State {
                        when: dragPan.drag.active
                        ParentChange { target: pitem; parent: window; } //设置此代码后可以在最上层不过释放鼠标不能恢复显示了
                        AnchorChanges { target: pitem; anchors.verticalCenter: undefined; anchors.horizontalCenter: undefined }
                        PropertyChanges { target: pitem; z: 100 }
                    }
                    Image {
                        width: 20; height: 20;
                        source: "resource/" + (modelData.isdir?"box.png":"file.png")
                    }
                    Text {
                        anchors.left: parent.left; anchors.leftMargin: 25;
                        text: modelData.server_filename
                    }
                }
            }
            MouseArea {
                id: dragPan;
                property int panDragIndex: -1
                anchors.fill: parent
                acceptedButtons: Qt.LeftButton | Qt.RightButton
                onPressed: function(mouse){
                    console.log("onpressed", dragPan.drag.target);
                    let lt= pandir.itemAt(mouse.x, mouse.y);
                    if(lt !== null){
                        panDragIndex = pandir.indexAt(mouse.x, mouse.y);
                        dragPan.drag.target = lt.children[0];
                        //console.log("onPressed begin", dragItem, dragItem.parent, dragItem.anchors.verticalCenter, dragItem.x, dragItem.y);
                    }else{
                        dragPan.drag.target = null;
                    }
                }
                onReleased: {
                    console.log("onReleased", dragPan.drag.target)
                    if(dragPan.drag.target?.Drag?.target === localDragTarget){ //网盘拖到本地
                        console.log("pantolocal", panDragIndex);
                        pandir.currentIndex = panDragIndex;
                        downBt.clicked();
                    }else if(dragPan.drag.active){ //没拖到指定位置也要强制刷新不然乱
                        let md = pandir.model;
                        pandir.model = [];
                        pandir.model = md;
                        pandir.currentIndex = -1;
                    }
                    dragPan.drag.target = null;
                }
                onClicked: function(mouse){
                    let index = pandir.indexAt(mouse.x, mouse.y);
                    if(mouse.button === Qt.RightButton){
                        pandir.currentIndex = index;
                        rightmenu.popup();
                    }else{
                        if(pandir.currentIndex === index){
                            pandir.currentIndex = -1;
                        }else{
                            pandir.currentIndex = index;
                        }
                    }
                }
                onDoubleClicked: function(mouse){
                    let index = pandir.indexAt(mouse.x, mouse.y);
                    if(index === -1) return;
                    if(pandir.model[index].isdir){
                        spath += (spath=="/"?"":"/") + pandir.model[index].server_filename;
                        panpath.model = spath.split("/");
                        //显示目录下内容
                        //let list = cpp_pan.list(spath);
                        //pandir.model = JSON.parse(list).list;
                        panreload.clicked();
                    }
                }
            }

            DropArea {
                id: panDragTarget
                anchors.fill: parent
                width: parent.width
                height: parent.height
                keys: ["draglocal","text/uri-list"] //接收显示的本地目录或者文件浏览器拖过来的文件
                Rectangle {
                    id: panDragRectangle
                    anchors.fill: parent
                    opacity: 0.3
                    visible: false
                    states: [
                        State {
                            when: panDragTarget.containsDrag
                            PropertyChanges {
                                target: panDragRectangle
                                color: "grey"
                                visible: true
                            }
                        },
                        State {
                            when: panDragTarget.containsDrag == false
                            PropertyChanges {
                                target: panDragRectangle
                                color: "white"
                                visible: false
                            }
                        }
                    ]
                }
                onDropped: function(drag){ //这个只有文件浏览器拖过来的才能触发次事件
                    console.log("onDropped", drag);
                    if(spath.indexOf("apps/" + cpp_pan.pan_name) == -1){
                        htooltip.show("上传目录只能是 " + "/apps/" + cpp_pan.pan_name);
                        if(!drag.hasUrls){
                            let md = localdir.model;
                            localdir.model = [];
                            localdir.model = md;
                            localdir.currentIndex = -1;
                        }
                        return;
                    }
                    if(drag.hasUrls){ //文件系统拖过来的
                        for(let localurl of drag.urls){
                            localurl = localurl.toString();
                            dragUploadPath = localurl.substr(8, localurl.length - 8);
                            uploadBt.clicked();
                        }
                        dragUploadPath = "";
                    }else{
                        uploadBt.clicked();
                    }
                }
                onEntered: function(drag){
                    console.log("onEntered", drag.urls.length);
                }
                onExited: function(){
                    console.log("onExited")
                    //拖动结束了才能刷新 放在这强制刷新listview就不会乱了
                    if(dragLocal.drag.target == null){
                        let md = localdir.model;
                        localdir.model = [];
                        localdir.model = md;
                        localdir.currentIndex = -1;
                    }
                }
                onPositionChanged: function(mouse){
                    //console.log("onPositionChanged", mouse.x, mouse.y);
                    pandir.currentIndex = pandir.indexAt(mouse.x, mouse.y);
                }
            }
        }
    }

    //初始化界面
    function init(){
        let ret = cpp_pan.info();
        //console.log(ret);
        let jd = JSON.parse(ret);
        if(jd.errno){
            console.log(ret);
            baiduname.text = "出错了";
            if(jd.errno === -6){ //登录信息过期了
                showLogin();
            }
        }else{
            baiduname.text = jd.baidu_name;
            avatar_url.source = jd.avatar_url;
            //获取本地当前目录
            lpath = cpp_local.currentPath();
            localpath.model = lpath.split("/").reverse();
            //显示本地目录下内容
            let list = cpp_local.list(lpath);
            //console.log(list);
            localdir.model = JSON.parse(list);
            localdir.currentIndex = -1;
            //显示盘目录
            panpath.model = [""];
            list = cpp_pan.list(spath);
            //console.log(list);
            pandir.model = JSON.parse(list).list;
            pandir.currentIndex = -1;
        }
    }

    Component.onCompleted: {
        if(window.visible){
            init();
        }
    }

    onVisibleChanged: {
        if(window.visible){
            init();
        }
    }
}
